<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Staff extends Model
{





    // 表名
    protected $name = 'staff';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    // 追加属性
    protected $append = [
        'service_status_text'
    ];




    public function getServiceStatusList()
    {
        return [0 => '空闲', 1 => '预约占用',2=>'其他占用',3=>'待上钟',4=>'服务中',5=>'超时'];
    }

    public function getServiceStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['service_status'];
        $list = $this->getServiceStatusList();
        return $list[$value] ?? '';
    }


    //修改员工的状态
    /*public function edit($staff_id,$service_status)
    {
        //服务状态,0空闲,1预约占用,2其他占用,3待上钟,4服务中,5超时
        $staff = Staff::get($staff_id);
        $staff->service_status = $service_status;

        $res = $staff->save();
        return $res;
    }

    //上钟修改员工的状态和下钟时间
    public function editServiceStatus($order_id,$staff_id)
    {
        $info = Order::get($order_id);

        $staff = Staff::get($staff_id);

        $project = StoreProject::get($info['project_id']);

        $minute = $project['hour']*60+$project['minute'];

        $total_minute = bcmul($minute,$info['num'],2);

        $date_timestamp = strtotime($info['date']);

        $work_end_time = bcadd($date_timestamp,$total_minute);

        $info->work_end_time = $work_end_time;

        $info->save();

        $staff->work_end_time = $work_end_time;

        $staff->service_status = 4;

        $res = $staff->save();

        return $res;
    }

    //加钟修改员工的下钟时间
    public function editWorkEndTime($order_id,$project_id,$num,$staff_id)
    {
        $info = Order::get($order_id);

        $staff = Staff::get($staff_id);

        $project = StoreProject::get($project_id);

        $minute = $project['hour']*60+$project['minute'];

        $total_minute = bcmul($minute,$num,2);

        $date_timestamp = $info->work_end_time;

        $work_end_time = bcadd($date_timestamp,$total_minute);

        $info->work_end_time = $work_end_time;

        $info->save();

        $staff->work_end_time = $work_end_time;

        $res = $staff->save();

        return $res;
    }


    //计算员工账单
    public function bill($start_date,$end_date,$self,$staff_id)
    {
        if ($staff_id){
            $search_staff_id = $staff_id;
        } else {
            $search_staff_id = $self;
        }

        if (!$start_date&&!$end_date){
            $start_date_timestamp = strtotime('today');
            $end_date_timestamp = strtotime('tomorrow') -1;
        } else {
            $start_date_timestamp = strtotime($start_date);
            $end_date_timestamp = strtotime($end_date);
        }


        $stat_model = new Stat();

        //钟数
        $num = $stat_model->projectNum($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //刷卡
        $swiping_card_price = $stat_model->swipingCardPrice($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //刷卡小费
        $swiping_card_tip_price = $stat_model->swipingCardTipPrice($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //现金
        $cash_price = $stat_model->cashPrice($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //礼品卡
        $gift_card_price = $stat_model->giftCardPrice($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //礼品卡小费
        $gift_card_tip_price = $stat_model->giftCardTipPrice($search_staff_id,$start_date_timestamp,$end_date_timestamp);

        //实际营业额（大费）=刷卡+礼品卡+现金
        $turnover = $swiping_card_price+$gift_card_price+$cash_price;

        //每日支票=（刷卡+礼卡+现金+小费）÷2
        $all_income = $swiping_card_price+$gift_card_price+$cash_price+$swiping_card_tip_price+$gift_card_tip_price;
        $cheque = bcdiv($all_income,2,2);

        //每日差价（技师收益）：（刷卡+礼品卡+现金）/2-（刷卡+礼卡+刷卡小费+礼品卡小费）
        $normal_income = bcdiv($turnover,2,2);//（刷卡+礼品卡+现金）/2
        $swiping_gift_income  = $swiping_card_price+$swiping_card_tip_price+$gift_card_price+$gift_card_tip_price;//（刷卡+礼卡+刷卡小费+礼品卡小费）
        $price_spread = bcsub($normal_income,$swiping_gift_income,2);

        $data = [
            'num'=>$num,//钟数
            'swiping_card_price'=>$swiping_card_price,//刷卡
            'swiping_card_tip_price'=>$swiping_card_tip_price,//刷卡小费
            'cash_price'=>$cash_price,//现金
            'gift_card_price'=>$gift_card_price, //礼品卡
            'gift_card_tip_price'=>$gift_card_tip_price,//礼品卡小费
            'cheque'=>$cheque,//每日支票
            'price_spread'=>$price_spread,//每日差价
            'turnover'=>$turnover//营业额
        ];

        return $data;
    }


    //计算门店员工账单
    public function storebill($start_date,$end_date,$store_id,$staff_id)
    {

        if (!$start_date&&!$end_date){
            $start_date_timestamp = strtotime('today');
            $end_date_timestamp = strtotime('tomorrow') -1;
        } else {
            $start_date_timestamp = strtotime($start_date);
            $end_date_timestamp = strtotime($end_date);
        }


        $stat_model = new Stat();

        //钟数
        $num = $stat_model->projectNum($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //刷卡
        $swiping_card_price = $stat_model->swipingCardPrice($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //刷卡小费
        $swiping_card_tip_price = $stat_model->swipingCardTipPrice($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //现金
        $cash_price = $stat_model->cashPrice($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //礼品卡
        $gift_card_price = $stat_model->giftCardPrice($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //礼品卡小费
        $gift_card_tip_price = $stat_model->giftCardTipPrice($staff_id,$start_date_timestamp,$end_date_timestamp,$store_id);

        //实际营业额（大费）=刷卡+礼品卡+现金
        $turnover = $swiping_card_price+$gift_card_price+$cash_price;

        //每日支票=（刷卡+礼卡+现金+小费）÷2
        $all_income = $swiping_card_price+$gift_card_price+$cash_price+$swiping_card_tip_price+$gift_card_tip_price;
        $cheque = bcdiv($all_income,2,2);

        //每日差价（技师收益）：（刷卡+礼品卡+现金）/2-（刷卡+礼卡+刷卡小费+礼品卡小费）
        $normal_income = bcdiv($turnover,2,2);//（刷卡+礼品卡+现金）/2
        $swiping_gift_income  = $swiping_card_price+$swiping_card_tip_price+$gift_card_price+$gift_card_tip_price;//（刷卡+礼卡+刷卡小费+礼品卡小费）
        $price_spread = bcsub($normal_income,$swiping_gift_income,2);

        $data = [
            'num'=>$num,//钟数
            'swiping_card_price'=>$swiping_card_price,//刷卡
            'swiping_card_tip_price'=>$swiping_card_tip_price,//刷卡小费
            'cash_price'=>$cash_price,//现金
            'gift_card_price'=>$gift_card_price, //礼品卡
            'gift_card_tip_price'=>$gift_card_tip_price,//礼品卡小费
            'cheque'=>$cheque,//每日支票
            'price_spread'=>$price_spread,//每日差价
            'turnover'=>$turnover//营业额
        ];

        return $data;
    }*/

}
